package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
@Slf4j
public class ArticleUpDownListener {

    @Autowired
    private IApArticleService apArticleService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "article.topic", type = ExchangeTypes.TOPIC), //交换机
            value = @Queue("article.queue"), //队列
            key = "article.up_or_down" //key
    ))
    public void upDown(HashMap<String, Object> map) {
        System.out.println("接收到上下架消息:" + JSON.toJSONString(map));
        Long articleId = (Long) map.get("articleId");
        Boolean enable = (Boolean) map.get("enable");

        ApArticle apArticle = new ApArticle();
        apArticle.setId(articleId);
        apArticle.setIsDown(enable);
        //update ap_article set is_down=?? where id=??
        apArticleService.updateById(apArticle);
    }
}
